<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class Remit extends Base
{
    protected $type = [
        'examine_time' => 'timestamp',
    ];

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    protected function getAccountAttr($value): string
    {
        return get_pay_way($value);
    }

    protected function getPayWayAttr($value): string
    {
        return get_pay_way($value);
    }

    protected function getStatusAttr($value): string
    {
        return getExamineStatus($value);
    }

    public static function _save($param, $logs): array
    {
        parent::startTrans();
        try {
            if (!isset($param['status'])) {
                throw new \Exception('请选择审核类型');
            }
            $info = parent::where(['id' => $param['id']])->find();
            if ($info->isEmpty()) {
                throw new \Exception('审核失败');
            }
            if ($info->getData('status') != 1) {
                throw new \Exception('此笔记录已审核，请勿重复审核！');
            }
            if ($param['status'] == 2) {
                $info->reject_msg = '';
                $msg = '通过充值申请,标识ID【' . $param['id'] . '】';
                Target::Account()->basics($info->uid, $info->getData('account'), $info->amount, '充值', '+')
                    ->createNormal();
            } else {
                $info->reject_msg = $param['reject_msg'];
                $msg = '驳回充值申请,标识ID【' . $param['id'] . '】';
            }
            $info->status = $param['status'];
            $info->admin_id = $logs['admin']->id;
            $info->admin_name = $logs['admin']->admin_name;
            $info->examine_time = time();
            $info->save();
            Logs::adds($msg, $logs['admin']->id, $logs['admin']->admin_name);

            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '已审核'];
    }

    public static function batchPass($ids, $admin): array
    {
        parent::startTrans();
        try {
            $list = parent::where(['status' => 1])->whereIn('id', array_values($ids))->select();
            if ($list->isEmpty()) {
                throw new \Exception('未查询到待审核的数据，请检查！');
            }
            $new_ids = [];
            foreach ($list as $info) {
                Target::Account()->basics($info->uid, $info->getData('account'), $info->amount, '充值', '+')
                    ->createNormal();
                $new_ids[] = $info->id;
            }
            if ($new_ids) {
                parent::update(
                    ['status' => 2, 'examine_time' => time(), 'admin_id' => $admin->id, 'admin_name' => $admin->admin_name],
                    ['id' => $new_ids]
                );
                $msg = '批量通过充值申请,标识ID【' . implode(',', $new_ids) . '】';
                Logs::adds($msg, $admin['id'], $admin['admin_name']);
            }

            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '已批量通过'];
    }

    public static function batchReject($ids, $admin): array
    {
        parent::startTrans();
        try {
            $list = parent::where(['status' => 1])->whereIn('id', array_values($ids))->select();
            if ($list->isEmpty()) {
                throw new \Exception('未查询到待审核的数据，请检查！');
            }
            $new_ids = [];
            foreach ($list as $info) {
                $new_ids[] = $info->id;
            }
            if ($new_ids) {
                parent::update(
                    ['status' => 3, 'examine_time' => time(), 'admin_id' => $admin->id, 'admin_name' => $admin->admin_name],
                    ['id' => $new_ids]
                );
                $msg = '批量驳回充值申请,标识ID【' . implode(',', $new_ids) . '】';
                Logs::adds($msg, $admin['id'], $admin['admin_name']);
            }

            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '已批量驳回'];
    }

    public static function add($param, $user, $system): array
    {
        try {
            if (!$system['remit_open']) {
                throw new \Exception('充值功能已关闭');
            }
            if ($param['amount'] < $system['remit_min']) {
                throw new \Exception('最低充值金额：' . $system['remit_min']);
            }
            if ($system['remit_bs']) {
                if ($param['amount'] % $system['remit_bs'] != 0) {
                    throw new \Exception('充值倍数：' . $system['remit_bs']);
                }
            }
            $sn = date('YmdHis') . rand(100000, 999999) . $user->id;
            parent::create([
                'sn' => $sn,
                'uid' => $user->id,
                'account' => $param['account'],
                'pay_way' => $param['pay_way'],
                'amount' => $param['amount'],
                'images' => $param['images']
            ]);

        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核'];
    }
}